﻿using System;
using System.IO;
using System.Text;
using MEF_Test.Module.AudioStation.Tests.Properties;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MEF_Test.Module.AudioStation.Tests
{
    /// <summary>
    /// Summary description for IntegrationTests
    /// </summary>
    [TestClass]
    public class IntegrationTests
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        
        [TestMethod]
        [Ignore]
        public void GetMp3FileStream()
        {
            // set a static file Res :
            const string res = "/volume1/music/iTunes Library/Phish/2009-12-29 - American Airlines Arena, Miami, Florida, USA (2009) CD2/03 Prince Caspian.mp3";
            const long size = 17242989;

            // 1. query File
            // 2. Save file to memory stream Asynchronously
            // 3. When the file is copied completely : call a callback (Or raise an event : let's think about what's best)
            // 4. Check that the file content matches the expectations


            var syno = new Synology.AudioStationApi.AudioStationSession(Settings.Default.SynoHost, Settings.Default.SynoPort);
            syno.Login(Settings.Default.Login, Settings.Default.Password);
            using (Stream ms = new MemoryStream())
            {
                Stream remoteStream;



                // We can't use the using statement while passing "out" and "ref" arguments
                syno.GetFileStream(res, out remoteStream);

                // Can we even read the stream ?
                Assert.IsTrue(remoteStream.CanRead);

                while (ms.Length < size)
                {
                    ms.WriteByte((byte)remoteStream.ReadByte());
                }

                // MemoryStream memStream = new MemoryStream(); 
                // byte[] buffer = new byte[2048]; 
                // int bytesRead = 0; 
                // do 
                // { 
                //     bytesRead = _ResponseStream.Read(buffer, 0, buffer.Length); 
                //     memStream.Write(buffer, 0, bytesRead); 
                // } while (bytesRead != 0); 


                // Check the filesize.
                Assert.AreEqual(size, ms.Position);

                // Don't forget to close and dispose as we're not in a using block.
                remoteStream.Close();
                remoteStream.Dispose();
                ms.Flush();
                ms.Position = 0;

                Assert.IsFalse(Encoding.ASCII.GetString(((MemoryStream)ms).ToArray()).StartsWith("<html>", StringComparison.InvariantCultureIgnoreCase), "The mp3 was not returned : instead, an html file was returned by the server.");

            }


        }

    }
}
